VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "DrawClass"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim DX As New DirectX7

Public DD As DirectDraw7
Dim DD_Surf As DirectDrawSurface7
Dim DD_Display As DirectDrawSurface7
Dim Clipper As DirectDrawClipper
Dim SurfSize As RECT
Dim FPS As Integer

Public ShowDebug As Boolean

Dim RenderHwnd As Long

Public Property Get Size() As RECT
    Size = SurfSize
End Property

Public Property Get Surf() As DirectDrawSurface7
    Set Surf = DD_Surf
End Property

Private Sub Class_Initialize()
    Dim DD_SurfD As DDSURFACEDESC2
    Dim DD_DisplayD As DDSURFACEDESC2
    Set DD = DX.DirectDrawCreate("")
    SurfSize.Right = 640
    SurfSize.Bottom = 640

End Sub

Public Sub SetWindow(hwnd As Long)
    Dim DD_SurfD As DDSURFACEDESC2
    Dim DD_DisplayD As DDSURFACEDESC2
    RenderHwnd = hwnd
    Call DD.SetCooperativeLevel(RenderHwnd, DDSCL_NORMAL)
    
    DD_SurfD.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
    DD_SurfD.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_VIDEOMEMORY
    DD_SurfD.lHeight = SurfSize.Bottom
    DD_SurfD.lWidth = SurfSize.Right
    Set DD_Surf = DD.CreateSurface(DD_SurfD)
    
    DD_DisplayD.lFlags = DDSD_CAPS
    DD_DisplayD.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE
    Set DD_Display = DD.CreateSurface(DD_DisplayD)
    
    Set Clipper = DD.CreateClipper(0)
    Clipper.SetHWnd hwnd

    DD_Display.SetClipper Clipper
End Sub

Public Sub SetSize(W As Long, H As Long)
    SurfSize.Right = W
    SurfSize.Bottom = H
End Sub

Public Sub Clear(Color As Long)
    Call DD_Surf.BltColorFill(Size, Color)
End Sub

Public Sub BltSurf()
    Static LastFpsUpdate As Long
    Static FpsTick As Long
    Dim oldcolor As Long
    Dim WindowRect As RECT
    Call DX.GetWindowRect(RenderHwnd, WindowRect)

    If Abs(GetTickCount - LastFpsUpdate) >= 100 Then
        FPS = Round(1000 / (Abs(GetTickCount - FpsTick) + 1), 2)
        LastFpsUpdate = GetTickCount
    End If
    
    If ShowDebug Then
        oldcolor = Surf.GetForeColor
        Call Surf.SetForeColor(255)
        Call DD_Surf.DrawText(0, 0, "W:" & Size.Right & " H:" & Size.Bottom & " FPS:" & FPS, False)
        Call Surf.SetForeColor(oldcolor)
    End If

    Call DD_Display.Blt(WindowRect, DD_Surf, SurfSize, DDBLT_WAIT)
    FpsTick = GetTickCount
End Sub
